# src/infrastructure/sgbm_config.py
import pathlib
from src.infrastructure.config_manager import ConfigManager
from typing import Dict, Any


class SGBMConfig:
    def __init__(self, path: pathlib.Path) -> None:
        self._mgr = ConfigManager(path)

    # ---------------- 代理通用方法----------------
    def load(self) -> Dict[str, Any]:
        return self._mgr.load()

    def save(self, data: Dict[str, Any]) -> None:
        self._mgr.save(data)

    def get(self, key: str, default: Any = None) -> Any:
        return self._mgr.get(key, default)

    # ---------------- 高内聚: 仅暴露业务语义 ----------------
    def toMatcherParams(self) -> Dict[str, int]:
        return {
            "min_disparity": int(self.get("sgbm.min_disparity", 0)),
            "num_disparities": int(self.get("sgbm.num_disparities", 160)),
            "block_size": int(self.get("sgbm.block_size", 9)),
            "p1_scale": int(self.get("sgbm.p1_scale", 8)),
            "p2_scale": int(self.get("sgbm.p2_scale", 32)),
            "disp12_max_diff": int(self.get("sgbm.disp12_max_diff", -1)),
            "pre_filter_cap": int(self.get("sgbm.pre_filter_cap", 31)),
            "uniqueness_ratio": int(self.get("sgbm.uniqueness_ratio", 10)),
            "speckle_window_size": int(self.get("sgbm.speckle_window_size", 100)),
            "speckle_range": int(self.get("sgbm.speckle_range", 32)),
            "mode": int(self.get("sgbm.mode", 2)),
        }

    def toPostParams(self) -> Dict[str, Any]:
        return {
            "use_wls": bool(self.get("post_process.use_wls", True)),
            "wls_lambda": float(self.get("post_process.wls_lambda", 8000.0)),
            "wls_sigma": float(self.get("post_process.wls_sigma", 1.5)),
            "use_speckle": bool(self.get("post_process.use_speckle", True)),
            "speckle_max_size": int(self.get("post_process.speckle_max_size", 100)),
            "use_hierarchy_fill": bool(
                self.get("post_process.use_hierarchy_fill", True)
            ),
        }
